package com.duk.main;
import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.duk.util.SearchAdapter;
import com.rss_reader.data.RSSFeed;
import com.rss_reader.data.RSSItem;
import com.rss_reader.data.StaticURL;
import com.rss_reader.sax.RSSHandler;
public class MainActivity extends ActivityGroup implements OnItemClickListener, Runnable, View.OnClickListener
{
    
	public final String RSS_URL = StaticURL.RSS_NEWEST_URL;
	public static LinearLayout container;
	public static View topBar;
	public final String tag = "生活新报";
	private RSSFeed feed = null;
	public View newest_layout;
	public View news_layout;  
	public View favorite_layout;
	public View comments_layout;
	public View mores_layout;
	private ListView itemlist;
	public SearchAdapter adapter;
	public ProgressDialog dialog;
	public static ImageView refresh;	
	public static ProgressBar refresh_bar;
	List<Map<String, Object>> contents;
	private ProgressDialog progressDlg;
	private HorizontalScrollView myhorizontalscrollview;
	public static View horizontal_scrollview;
	public String activity = null;
	public TextView channelTitle;
	public String type = "china";
	TextView lianghui;
	TextView china;
	TextView yunnan;
	TextView happy;
	TextView international;
	TextView hot;
	TextView supplement;
	TextView sports;
	TextView money;
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			progressDlg.dismiss();			
			if(msg.obj.toString().equals("NewsActivity")) {
				MainActivity.this.activity = "NewsActivity";
				 /* container.addView(getLocalActivityManager().startActivity(
	                        "Module1",
	                        new Intent(MainActivity.this, NewestActivity.class)
	                                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
	                        .getDecorView());	*/
				//Toast.makeText(getApplicationContext(), MainActivity.this.type+"dfd", Toast.LENGTH_LONG).show();
				Intent localIntent1 = new Intent();
				
				// Intent localIntent11 = localIntent1.putExtra("back_title", str4);
				 	Intent localIntent12 = localIntent1.putExtra("type",  MainActivity.this.type);			  
			        //Intent localIntent12 = localIntent1.putExtra("news_url", StaticURL.RSS_NEWS_URL);			       
			        Intent localIntent13 = localIntent1.setClass(MainActivity.this, NewsActivity.class);
			        Window localWindow4 = getLocalActivityManager().startActivity("subActivity", localIntent1);
			        LinearLayout localLinearLayout4 = container;
			        View localView4 = localWindow4.getDecorView();
			        localLinearLayout4.addView(localView4);
			} else if(msg.obj.toString().equals("NewestActivity")) {
				MainActivity.this.activity = "NewestActivity";
			 container.addView(getLocalActivityManager().startActivity(
                     "Module2",
                     new Intent(MainActivity.this,NewestActivity.class)
                             .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                     .getDecorView());
			} else if(msg.obj.toString().equals("CommentsActivity")) {
				MainActivity.this.activity = "CommentsActivity";
				 container.addView(getLocalActivityManager().startActivity(
	                     "Module3",
	                     new Intent(MainActivity.this,CommentsActivity.class)
	                             .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
	                     .getDecorView());
				} else if(msg.obj.toString().equals("FavoriteActivity")) {
					MainActivity.this.activity = "FavoriteActivity";
				 container.addView(getLocalActivityManager().startActivity(
	                     "Module4",
	                     new Intent(MainActivity.this,FavoriteActivity.class)
	                             .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
	                     .getDecorView());
			}
			refresh.setVisibility(0);
			refresh_bar.setVisibility(8);
		}
		
	};
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);  
        itemlist = (ListView) findViewById(R.id.itemlist);
        if(!CheckNetwork()) {
        	Toast.makeText(getApplicationContext(), R.string.nonetwork, Toast.LENGTH_LONG).show();
        	return;
        }
        HorizontalScrollView localHorizontalScrollView = (HorizontalScrollView)findViewById(R.id.myhorizontalscrollview);
        this.myhorizontalscrollview = localHorizontalScrollView;
        horizontal_scrollview = findViewById(R.id.HorizontalScrollView01);
        horizontal_scrollview.setVisibility(8);
        refresh_bar = (ProgressBar)findViewById(R.id.refresh_progress);
        refresh_bar.setVisibility(8);
        refresh = (ImageView)findViewById(R.id.refresh);
        Intent localIntent1 = new Intent();
        refresh.setOnClickListener(this);
    
        WSAsyncTask task = new WSAsyncTask();
		task.execute();
		TextView localTextView = (TextView)findViewById(R.id.channelTitle);
		this.channelTitle = localTextView;
		this.channelTitle.setText(R.string.newest);
		initBottomBar();
		initLead();
    }
  
    private RSSFeed getFeed(String urlString)
    {
    	try
    	{
    	   URL url = new URL(urlString);

           SAXParserFactory factory = SAXParserFactory.newInstance();
           SAXParser parser = factory.newSAXParser();
           XMLReader xmlreader = parser.getXMLReader();           
           RSSHandler rssHandler = new RSSHandler();
           xmlreader.setContentHandler(rssHandler);
           InputSource is = new InputSource(url.openStream());             
           xmlreader.parse(is);
  
           return rssHandler.getFeed();
    	}
    	catch (Exception ee)
    	{
    			ee.printStackTrace();
    		return null;
    	}
    }

    private void initLead()
    {    	 
    	    TextView localTextView1 = (TextView)findViewById(R.id.china);
    	    this.china = localTextView1;
    	    this.china.setOnClickListener(this);    	    
    	    int i = getResources().getColor(R.color.red);
    	    localTextView1.setTextColor(i);
    	    TextView localTextView2 = (TextView)findViewById(R.id.yunnan);
    	    this.yunnan = localTextView2;
    	    this.yunnan.setOnClickListener(this);
    	    TextView localTextView3 = (TextView)findViewById(R.id.happy);
    	    this.happy = localTextView3;
    	    this.happy.setOnClickListener(this);
    	    TextView localTextView4 = (TextView)findViewById(R.id.international);
    	    this.international = localTextView4;
    	    this.international.setOnClickListener(this);
    	    TextView localTextView5 = (TextView)findViewById(R.id.hot);
    	    this.hot = localTextView5;
    	    this.hot.setOnClickListener(this);
    	    TextView localTextView6 = (TextView)findViewById(R.id.supplement);
    	    this.supplement = localTextView6;
    	    this.supplement.setOnClickListener(this);
    	    TextView localTextView7 = (TextView)findViewById(R.id.sports);
    	    this.sports = localTextView7;
    	    this.sports.setOnClickListener(this);
    	    TextView localTextView8 = (TextView)findViewById(R.id.money);
    	    this.money = localTextView8;
    	    this.money.setOnClickListener(this);
    	    
    }
    
    private void  setTextColor(int textId) {
    	 int i1 = getResources().getColor(R.color.red);
    	 int i2 = getResources().getColor(R.color.white);
    	 
    	 TextView localTextView1 = (TextView)findViewById(R.id.china);		
		 TextView localTextView2 = (TextView)findViewById(R.id.yunnan);
		 TextView localTextView3 = (TextView)findViewById(R.id.happy);		
		 TextView localTextView4 = (TextView)findViewById(R.id.international);
		 TextView localTextView5 = (TextView)findViewById(R.id.hot);		
		 TextView localTextView6 = (TextView)findViewById(R.id.supplement);
		 TextView localTextView7 = (TextView)findViewById(R.id.sports);		
		 TextView localTextView8 = (TextView)findViewById(R.id.money);
    	switch(textId) {
    		case 1:
    			 localTextView1.setTextColor(i1);
    			 localTextView2.setTextColor(i2);
    			 localTextView3.setTextColor(i2);
    			 localTextView4.setTextColor(i2);
    			 localTextView5.setTextColor(i2);
    			 localTextView6.setTextColor(i2);
    			 localTextView7.setTextColor(i2);
    			 localTextView8.setTextColor(i2);
    			 break;
    		case 2 :
    			localTextView1.setTextColor(i2);
   			 	localTextView2.setTextColor(i1);
	   			localTextView3.setTextColor(i2);
				localTextView4.setTextColor(i2);
				localTextView5.setTextColor(i2);
				localTextView6.setTextColor(i2);
				localTextView7.setTextColor(i2);
				localTextView8.setTextColor(i2);
    			break;
    		case 3 :
    			localTextView1.setTextColor(i2);
   			 	localTextView2.setTextColor(i2);
	   			localTextView3.setTextColor(i1);
				localTextView4.setTextColor(i2);
				localTextView5.setTextColor(i2);
				localTextView6.setTextColor(i2);
				localTextView7.setTextColor(i2);
				localTextView8.setTextColor(i2);
    			break;
    		case 4 :
    			localTextView1.setTextColor(i2);
   			 	localTextView2.setTextColor(i2);
	   			localTextView3.setTextColor(i2);
				localTextView4.setTextColor(i1);
				localTextView5.setTextColor(i2);
				localTextView6.setTextColor(i2);
				localTextView7.setTextColor(i2);
				localTextView8.setTextColor(i2);
    			break;
    		case 5 :
    			localTextView1.setTextColor(i2);
   			 	localTextView2.setTextColor(i2);
	   			localTextView3.setTextColor(i2);
				localTextView4.setTextColor(i2);
				localTextView5.setTextColor(i1);
				localTextView6.setTextColor(i2);
				localTextView7.setTextColor(i2);
				localTextView8.setTextColor(i2);
    			break;
    		case 6 :
    			localTextView1.setTextColor(i2);
   			 	localTextView2.setTextColor(i2);
	   			localTextView3.setTextColor(i2);
				localTextView4.setTextColor(i2);
				localTextView5.setTextColor(i2);
				localTextView6.setTextColor(i1);
				localTextView7.setTextColor(i2);
				localTextView8.setTextColor(i2);
    			break;
    		case 7 :
    			localTextView1.setTextColor(i2);
   			 	localTextView2.setTextColor(i2);
	   			localTextView3.setTextColor(i2);
				localTextView4.setTextColor(i2);
				localTextView5.setTextColor(i2);
				localTextView6.setTextColor(i2);
				localTextView7.setTextColor(i1);
				localTextView8.setTextColor(i2);
    			break;
    		case 8:
    			localTextView1.setTextColor(i2);
   			 	localTextView2.setTextColor(i2);
	   			localTextView3.setTextColor(i2);
				localTextView4.setTextColor(i2);
				localTextView5.setTextColor(i2);
				localTextView6.setTextColor(i2);
				localTextView7.setTextColor(i2);
				localTextView8.setTextColor(i1);
    			break;
    		case 9:
    			localTextView1.setTextColor(i2);
   			 	localTextView2.setTextColor(i2);
	   			localTextView3.setTextColor(i2);
				localTextView4.setTextColor(i2);
				localTextView5.setTextColor(i2);
				localTextView6.setTextColor(i2);
				localTextView7.setTextColor(i2);
				localTextView8.setTextColor(i2);
    			break;
    	}
      
	    
    }
    private void showListView(RSSFeed feedSoruce)
    {
        
        if (feedSoruce == null)
        {
        	Toast.makeText(getApplicationContext(), "读取数据失败，请重试", Toast.LENGTH_LONG).show();
        	return;
        }
        contents =feedSoruce.getAllItemsForListView();
        adapter = new SearchAdapter(MainActivity.this,
        		contents, R.layout.listitem, 
				new String[] { RSSItem.TITLE, RSSItem.DESCRIPTION, RSSItem.IMAGE_PATH }, new int[] {
						R.id.listitem_title, R.id.listitem_content,
						R.id.listitem_pic });
        itemlist.setAdapter(adapter);
        itemlist.setOnItemClickListener(this);  
        itemlist.setSelection(0);
        
    }
    
    public void initBottomBar() {
    	View localView1 = findViewById(R.id.newest_layout);
    	this.newest_layout = localView1;
    	this.newest_layout.setBackgroundResource(R.drawable.tab_selected_bg);
        container = (LinearLayout) findViewById(R.id.LinearLayout02);
        topBar = (View)findViewById(R.id.top_bar); 
        
        this.newest_layout.setOnClickListener(this);
        View localView2 = findViewById(R.id.news_layout);
        this.news_layout = localView2;
        this.news_layout.setOnClickListener(this);

        View localView3 = findViewById(R.id.favorite_layout);
        this.favorite_layout = localView3;
        this.favorite_layout.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View arg2) {
				  setBackground(3);
				  topBar.setVisibility(8);
				  container.removeAllViews();
				  channelTitle.setText(R.string.favorite);
				  loadMessage("FavoriteActivity");	
			}			
		});
        
        
        View localView4 = findViewById(R.id.comments_layout);
        this.comments_layout = localView4;
        this.comments_layout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setBackground(4);
				topBar.setVisibility(8);
				container.removeAllViews();
				channelTitle.setText(R.string.comments);
				loadMessage("CommentsActivity");
			}
		});
        
        View localView5 = findViewById(R.id.mores_layout);
        this.mores_layout = localView5;
        this.mores_layout.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View arg4) {
				setBackground(5);
				container.removeAllViews();
				channelTitle.setText(R.string.mores);
				  container.addView(getLocalActivityManager().startActivity(
	                        "Module5",
	                        new Intent(MainActivity.this, MoreActivity.class)
	                                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
	                        .getDecorView());	 
			}
		});
    	
    }    
    
    public void setBackground(int type) {
    	switch(type) {
    		case 1:
    			this.newest_layout.setBackgroundResource(R.drawable.tab_selected_bg);
    			this.news_layout.setBackgroundResource(0);
    			this.favorite_layout.setBackgroundResource(0);
    			this.comments_layout.setBackgroundResource(0);
    			this.mores_layout.setBackgroundResource(0);
    			break;
    		case 2:
    			this.newest_layout.setBackgroundResource(0);
    			this.news_layout.setBackgroundResource(R.drawable.tab_selected_bg);
    			this.favorite_layout.setBackgroundResource(0);
    			this.comments_layout.setBackgroundResource(0);
    			this.mores_layout.setBackgroundResource(0);
    			break;
    		case 3:
    			this.newest_layout.setBackgroundResource(0);
    			this.news_layout.setBackgroundResource(0);
    			this.favorite_layout.setBackgroundResource(R.drawable.tab_selected_bg);
    			this.comments_layout.setBackgroundResource(0);
    			this.mores_layout.setBackgroundResource(0);
    			break;
    		case 4:    			
    			this.newest_layout.setBackgroundResource(0);
    			this.news_layout.setBackgroundResource(0);
    			this.favorite_layout.setBackgroundResource(0);
    			this.comments_layout.setBackgroundResource(R.drawable.tab_selected_bg);
    			this.mores_layout.setBackgroundResource(0);
    			break;
    		case 5:    			
    			this.newest_layout.setBackgroundResource(0);
    			this.news_layout.setBackgroundResource(0);
    			this.favorite_layout.setBackgroundResource(0);
    			this.comments_layout.setBackgroundResource(0);
    			this.mores_layout.setBackgroundResource(R.drawable.tab_selected_bg);
    			break;
    	}
    }
   
     public void onItemClick(AdapterView parent, View v, int position, long id)
     {
    	 Intent itemintent = new Intent(this,ActivityShowDescription.class);
         
    	 Bundle b = new Bundle();
    	 b.putString("title", feed.getItem(position).getTitle());
    	 b.putString("news_type", feed.getItem(position).getCategory());
    	 b.putString("description", feed.getItem(position).getDescription());
    	 b.putString("link", feed.getItem(position).getLink());
    	 b.putString("pubdate", feed.getItem(position).getPubDate());
    	 b.putString("content", feed.getItem(position).getContent());    	
    	 b.putSerializable("image_path", feed.getItem(position).getImage_path());
    	 itemintent.putExtra("android.intent.extra.rssItem", b);
         startActivityForResult(itemintent, 0);
     }
     
     class WSAsyncTask extends AsyncTask {

		@Override
		protected Object doInBackground(Object... arg0) {
			feed = getFeed(RSS_URL);
			return feed;
		}

		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(Object result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			setProgressBarIndeterminateVisibility(false);
			dialog.dismiss();			
			showListView((RSSFeed)result);
			MainActivity.this.activity = "NewestActivity";
		}

		@Override
		protected void onPreExecute() {			
			super.onPreExecute();
			dialog = new ProgressDialog(MainActivity.this);
			dialog.setCancelable(false);
			dialog.setTitle("数据加载中...");
			dialog.setMessage("加载中,请稍候...");
			dialog.show();
		}

		@Override
		protected void onProgressUpdate(Object... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			
		}
    	 
     }
     
     private void loadMessage(String activity) {
    	 	this.activity = activity;
			progressDlg = ProgressDialog.show(MainActivity.this, "数据加载中...", "加载中,请稍候...", true);
			Thread t = new Thread(MainActivity.this);
			t.start();
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				Thread.sleep(1000);
				Message msg = handler.obtainMessage(1, 1, 1, activity);
				handler.sendMessage(msg);
			} catch(InterruptedException e) {
				e.printStackTrace();
			}
		}  
     
     private Dialog ExitDialog(Context context) {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setIcon(R.drawable.icon);
			builder.setTitle("生活新报提示");
			builder.setMessage("确定退出么?");
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							finish();
						}
					});
			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {

						}
					});
			return builder.create();
		}
	   
	    @Override
		public boolean onKeyDown(int keyCode, KeyEvent event) {
			if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
				ExitDialog(MainActivity.this).show();
				return true;
			}
			return super.onKeyDown(keyCode, event);
		}
	   

		@Override
		public void onClick(View paramView) {
			getLocalActivityManager().removeAllActivities();
			container.removeAllViews();  
			topBar.setVisibility(1);
		    setAllTextToWhite();
			   
			if(paramView.getId() == R.id.newest_layout) {	
				MainActivity.this.activity = "NewestActivity";
				setBackground(1);
				Toast.makeText(getApplicationContext(), "newest", Toast.LENGTH_SHORT);
				this.channelTitle.setText(R.string.newest);
				horizontal_scrollview.setVisibility(8);
				loadMessage("NewestActivity");  
			} else if(paramView.getId() == R.id.news_layout) {
				MainActivity.this.activity = "NewsActivity";
				setBackground(2);
				setTextColor(1);
				this.channelTitle.setText(R.string.news);
				horizontal_scrollview.setVisibility(0);
				loadMessage("NewsActivity");
			} else if(paramView.getId() == R.id.comments_layout) {
				MainActivity.this.activity = "CommentsActivity";
				setBackground(4);
				horizontal_scrollview.setVisibility(8);
				this.channelTitle.setText(R.string.comments);
				loadMessage("CommentsActivity");
			} else if(paramView.getId() == R.id.china) {
				MainActivity.this.activity = "NewsActivity";
				//Toast.makeText(getApplicationContext(), "china", Toast.LENGTH_SHORT).show();
				this.type = "china";
				TextView localTextView1 = (TextView)findViewById(R.id.yunnan);
		    	this.yunnan = localTextView1;		 
		        setTextColor(1);
		        this.channelTitle.setText(R.string.china);
		        loadMessage("NewsActivity");
			} else if(paramView.getId() == R.id.yunnan) {
				MainActivity.this.activity = "NewsActivity";
				this.type = "yunnan";
				//Toast.makeText(getApplicationContext(), "yunnan", Toast.LENGTH_SHORT).show();
				TextView localTextView2 = (TextView)findViewById(R.id.yunnan);
	    	    this.yunnan = localTextView2;
	    	    setTextColor(2);
	    	    loadMessage("NewsActivity");
	    	    this.channelTitle.setText(R.string.yunnan);
	    	    
			} else if(paramView.getId() == R.id.happy) {
				MainActivity.this.activity = "NewsActivity";
				this.type = "happy";				
				TextView localTextVie3 = (TextView)findViewById(R.id.happy);
	    	    this.happy = localTextVie3;
	    	    setTextColor(3);
	    	    loadMessage("NewsActivity");
	    	    this.channelTitle.setText(R.string.happy);
	    	    
			} else if(paramView.getId() == R.id.international) {
				MainActivity.this.activity = "NewsActivity";
				this.type = "international";				
				TextView localTextVie4 = (TextView)findViewById(R.id.international);
	    	    this.international = localTextVie4;
	    	    setTextColor(4);
	    	    loadMessage("NewsActivity");
	    	    this.channelTitle.setText(R.string.international);
	    	    
			} else if(paramView.getId() == R.id.hot) {
				MainActivity.this.activity = "NewsActivity";
				this.type = "hot";				
				TextView localTextVie5 = (TextView)findViewById(R.id.hot);
	    	    this.hot = localTextVie5;
	    	    setTextColor(5);
	    	    loadMessage("NewsActivity");
	    	    this.channelTitle.setText(R.string.hot);
	    	    
			} else if(paramView.getId() == R.id.supplement) {
				MainActivity.this.activity = "NewsActivity";
				this.type = "supplement";				
				TextView localTextVie6 = (TextView)findViewById(R.id.supplement);
	    	    this.supplement = localTextVie6;
	    	    setTextColor(6);
	    	    loadMessage("NewsActivity");
	    	    this.channelTitle.setText(R.string.supplement);
	    	    
			} else if(paramView.getId() == R.id.sports) {
				MainActivity.this.activity = "NewsActivity";
				this.type = "sports";				
				TextView localTextVie7 = (TextView)findViewById(R.id.sports);
	    	    this.supplement = localTextVie7;
	    	    setTextColor(7);
	    	    loadMessage("NewsActivity");
	    	    this.channelTitle.setText(R.string.sports);
	    	    
			}  else if(paramView.getId() == R.id.money) {
				MainActivity.this.activity = "NewsActivity";
				this.type = "money";				
				TextView localTextVie8 = (TextView)findViewById(R.id.money);
	    	    this.supplement = localTextVie8;
	    	    setTextColor(8);
	    	    loadMessage("NewsActivity");
	    	    this.channelTitle.setText(R.string.money);
	    	    
			} else if(paramView.getId() == R.id.refresh) {	
				setTextColor(9);
				refresh.setVisibility(8);
				refresh_bar.setVisibility(0);						
				loadMessage(MainActivity.this.activity);								
					
			}
	
		}
		 private void setAllTextToWhite()
		  {		   
		    TextView localTextView1 = this.china;
		    int j = getResources().getColor(R.color.text_white);
		    localTextView1.setTextColor(j);
		  
		  }

		 private boolean CheckNetwork() {
		        boolean flag = false;
		        ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		        if (cwjManager.getActiveNetworkInfo() != null)
		            flag = cwjManager.getActiveNetworkInfo().isAvailable();
		        if (!flag) {
		            Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络").setMessage(getResources().getString(R.string.nonetwork));
		            b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
		                public void onClick(DialogInterface dialog, int whichButton) {
		                    Intent mIntent = new Intent("/");
		                    ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
		                    mIntent.setComponent(comp);
		                    mIntent.setAction("<span class=\"hilite\">android</span>.intent.action.VIEW");
		                    startActivity(mIntent);
		                }
		            }).setNeutralButton("取消", new DialogInterface.OnClickListener() {
		                public void onClick(DialogInterface dialog, int whichButton) {
		                    dialog.cancel();
		                }
		            }).create();
		            b.show();
		        }
		 
		        return flag;
		    }
		
}